import { mapState, mapMutations } from 'vuex'
import {MD5} from '@/utils/MD5'
import storage from '@/utils/storage'
import moment from 'moment'
// 混合过滤器
export const vmfilter = {
  filters: {
    moneyFilter (input) {
      // money过滤器 默认为分，需要转换成元
      let result = '0.00'
      if (!/^\d+$/.test(input)) {
        return result
      }
      result = input / 100 + ''
      let aResult = result.split('.')
      if (!aResult[1]) {
        return `${result}.00`
      }
      if (!aResult[1][1]) {
        return `${result}0`
      }
      return result
    },
    moneyFloatFilter (input) {
      let result = '0.00'
      if (!/^\d+$/.test(input)) {
        return result
      }
      result = input / 100 + ''
      let aResult = result.split('.')
      if (!aResult[1]) {
        return `${result}.00`
      }
      if (!aResult[1][1]) {
        return `${result}0`
      }
      return result
    },
    /**
     * 全局时间过滤器
     * @param {*} input
     * @param {时间格式} sFormat
     */
    gDateFilter (input, sFormat) {
      sFormat = sFormat || 'YYYY-MM-DD HH:mm:ss'
      let result = moment(input).format(sFormat)
      if (result === 'Invalid date') {
        return ''
      }
      return result
    }
  }
}
export const vmdata = {
  data () {
    return {
      comShow: false, // 全局显示
      listHeight: '736px' // 混合器获取视窗高度，用户上拉加载
    }
  },
  created () {
  },
  mounted () {

  },
  computed: {
    ...mapState(['userInfo'])
  },
  methods: {
    ...mapMutations([
      'USER_INFO'
    ]),
    /**
     * 自定义路由导航
     * @param {路由导航} url
     */
    navTo (url) {
      if (typeof (url) === 'string') {
        url = url.replace('undefined', '')
      }
      this.$router.push(url)
    },
    /**
     * 隐藏分享
     */
    hideShareMenu () {
      let _This = this
      if (typeof WeixinJSBridge === 'undefined') {
        if (document.addEventListener) {
          document.addEventListener(
            'WeixinJSBridgeReady',
            _This.hideOptionMenu,
            false
          )
        } else if (document.attachEvent) {
          document.attachEvent('WeixinJSBridgeReady', _This.hideOptionMenu)
          document.attachEvent('onWeixinJSBridgeReady', _This.hideOptionMenu)
        }
      } else {
        _This.hideOptionMenu()
      }
    },
    /**
     * 显示分享
     */
    showShareMenu () {
      let _This = this
      if (typeof WeixinJSBridge === 'undefined') {
        if (document.addEventListener) {
          document.addEventListener(
            'WeixinJSBridgeReady',
            _This.showOptionMenu,
            false
          )
        } else if (document.attachEvent) {
          document.attachEvent('WeixinJSBridgeReady', _This.showOptionMenu)
          document.attachEvent('onWeixinJSBridgeReady', _This.showOptionMenu)
        }
      } else {
        _This.showOptionMenu()
      }
    },
    /**
     * 获取localstrage
     * @param {*} skey
     * @param {*} isObject
     */
    getLocalStorage (skey) {
      return storage.get(skey)
    },
    /**
     * 设置localstorage
     * @param {*} skey
     * @param {*} svalue
     * @param {*} isObject
     */
    setLocalStorage (skey, value = '', expire = 0) {
      storage.set(skey, value, expire)
    },
    /**
     * 当前cdkey
     */
    getCdkey (memberPhone = '') {
      memberPhone = memberPhone || this.userInfo.mobile
      if (!memberPhone) {
        return ''
      }
      let phoneVal = moment().format('MMDDYYYY') + memberPhone.substring(7) + (new Date().getDay() || 7)
      return MD5(phoneVal, 32)// .substring(0, 16)
    },
    hideOptionMenu () {
      WeixinJSBridge.call('hideOptionMenu')
    },
    showOptionMenu () {
      WeixinJSBridge && WeixinJSBridge.call('showOptionMenu')
    }
  },
  destroyed () {
    // this.showShareMenu();
  }
}
/**
 * 加载起始参数，接受前一个页面参数
 */
export const mixinParams = {
  created () {
    let _This = this
    _This.mixPath = `${location.origin}/#${_This.$route.path}`// 当前路由path
    let oMixParams = _This.$route.params
    for (const key in oMixParams) {
      if (oMixParams.hasOwnProperty(key)) {
        _This[key] = oMixParams[key] || ''
      }
    }
    let oMixQuery = this.$route.query
    for (const key in oMixQuery) {
      if (oMixQuery.hasOwnProperty(key)) {
        _This[key] = oMixQuery[key] || ''
      }
    }
  }

}
